# ----------------------------------------------------------------------
# ' this file uses opioid prescription files for each quarter
# ' to generate the total number of opioid prescriptions 
# ' by BK (bl11@indiana.edu)
# ----------------------------------------------------------------------

load_library = c('bit64','data.table','fst','future.apply','stringr','logger')
invisible(lapply(load_library, function(x) library(x, character.only=TRUE, quietly= TRUE)))
options(scipen=999)

bucket = file.path('/N','project','iuni_doctorshopping')

# read arguments
args=commandArgs(TRUE)
infile = args[[1]]
outfile = args[[2]]

data = fread(infile)
data[, QUANTITY := as.double(QUANTITY)]
data[, DAYS_SUP := as.double(DAYS_SUP)]
data[, mme := as.double(mme)]

udata = data[, .(
	n_opioid_prescription = .N,
	sum_opioid_quantity = sum(QUANTITY,na.rm=TRUE),
	sum_opioid_days =sum(DAYS_SUP,na.rm=TRUE),
	sum_opioid_mme = sum(mme, na.rm=TRUE)
	), by='PATID']

fwrite(udata, outfile)



